﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Views;
using EHR.Common;

namespace CommunitySystem
{
    public partial class PeiXunQingKuangWindow : AsyncCtrl
    {
        private PeiXunService.PeiXunServiceClient client1 = new PeiXunService.PeiXunServiceClient();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private int PeiXunID = 0;
        private int OrgID = 0;
        private string juese = "";
        public PeiXunQingKuangWindow(int peixunid,int orgid)
        {
            InitializeComponent();
            PeiXunID = peixunid;
            OrgID = orgid;
            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            client1.UpdatePeiXunDetailFaFangCompleted += new EventHandler<PeiXunService.UpdatePeiXunDetailFaFangCompletedEventArgs>(client1_UpdatePeiXunDetailFaFangCompleted);
            client1.GetPeiXunDetailModel2Completed += new EventHandler<PeiXunService.GetPeiXunDetailModel2CompletedEventArgs>(client1_GetPeiXunDetailModel2Completed);
            client1.GetPeiXunDetailModelCompleted += new EventHandler<PeiXunService.GetPeiXunDetailModelCompletedEventArgs>(client1_GetPeiXunDetailModelCompleted);
            client1.CancelDingPiaoCompleted+=new EventHandler<PeiXunService.CancelDingPiaoCompletedEventArgs>(client1_CancelDingPiaoCompleted);
            Load();
        }

        void client1_GetPeiXunDetailModel2Completed(object sender, PeiXunService.GetPeiXunDetailModel2CompletedEventArgs e)
        {
            DGRole_admin.ItemsSource = null;
            DGRole_admin.ItemsSource = e.Result;
        }
        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            juese = e.Result;
            if (e.Result != "人事干部")
            {
                CheckData.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                CheckData.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        public void Load()
        {
            client1.GetPeiXunDetailModel2Async(OrgID.ToString(), 0, PeiXunID);
        }

        void client1_GetPeiXunDetailModelCompleted(object sender, PeiXunService.GetPeiXunDetailModelCompletedEventArgs e)
        {
            DGRole_admin.ItemsSource = null;
            DGRole_admin.ItemsSource = e.Result;
        }

        private void CheckData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole_admin.SelectedItem as PeiXunService.PeiXunDetailModel;
            if (seljw != null)
            {
                if (MessageBox.Show("确定发放吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    client1.UpdatePeiXunDetailFaFangAsync(seljw.ID);
                }
            }
        }
        void client1_UpdatePeiXunDetailFaFangCompleted(object sender, PeiXunService.UpdatePeiXunDetailFaFangCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("发放成功！");
            Load();
        }

        private void CancelSavePeiZhiData_Click(object sender, RoutedEventArgs e)
        {
                var seljw = this.DGRole_admin.SelectedItem as PeiXunService.PeiXunDetailModel;
                if (seljw != null)
                {
                    if (seljw.Status == 1)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("该培训已结束，不可取消本次订票！");
                        return;
                    }
                    if (seljw.YiFaFang == true)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("该培训票已发放，不可取消本次订票！");
                        return;
                    }
                    if (MessageBox.Show("确定取消本次订票吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        client1.CancelDingPiaoAsync(seljw.ID);
                    }

                }
                else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择培训信息！");
           

        }

        void client1_CancelDingPiaoCompleted(object sender, PeiXunService.CancelDingPiaoCompletedEventArgs e)
        {
            //if (e.Result == true)
            //{
            //    SilverlightMessageBoxLibrary.Message.InfoMessage("取消成功！");
            //    string or = "0";
            //    if (orgids.Count > 0)
            //    {
            //        foreach (string i in orgids)
            //        {
            //            or += i + ",";
            //        }
            //        or = or.Substring(0, or.Length - 1);
            //    }
            //    LoadData(or);
            //}
            //else
            //{
            //    SilverlightMessageBoxLibrary.Message.ErrorMessage("该单位没有订票！");
            //}
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("取消成功！");
                Load();
            }

            
        }
    }
}
